package cn.tannn.cat.file.server.entity;

import cn.hutool.core.date.DateTime;
import cn.tannn.cat.file.api.entity.FileIndexVO;
import cn.tannn.cat.file.sdk.bean.FileIndex;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.time.format.DateTimeFormatter;

import static cn.tannn.cat.file.api.constants.CommonConstants.DEFAULT_FORMAT_DATETIME;

/**
 * 文件索引数据
 *
 * @author <a href="https://tannn.cn/">tan</a>
 * @date 2024/3/7 13:18
 */
@Entity
@Table(name = "file_index_meta")
@Comment("文件索引数据")
@Getter
@Setter
@ToString
@DynamicUpdate
@DynamicInsert
public class FileIndexMeta extends CommonBean<FileIndexMeta> {


    /**
     * 存储器配置id[FileStorage.id]
     * 对真实文件进行操作是需要拿到其存储的配置
     */
    @Comment("存储器配置id[FileConfig.id]")
    @Column(columnDefinition = "bigint not null")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long storageId;

    /**
     * 字典值[Dict.value] configId的冗余字段
     */
    @Comment("字典值[Dict.value] configId的冗余字段")
    @Column(columnDefinition = "varchar(100) not null")
    private String storage;


    /**
     * 原文件名
     */
    @Comment("原文件名")
    @Column(columnDefinition = "varchar(512)")
    private String originalName;

    /**
     * 新文件名
     */
    @Comment("新文件名")
    @Column(columnDefinition = "varchar(512) not null")
    private String freshName;

    /**
     * 文件路径(存储在设备里的真实路径) [本地磁盘是全路径，oss是不带bucket的全路径
     */
    @Comment("文件路径(存储在设备里的真实路径)")
    @Column(columnDefinition = "varchar(512) not null")
    private String path;

    /**
     * 子路径[文件除开根路径的子路径]
     * <p>本地文件根路径=uploadDir</p>
     * <p>s3根路径=桶</p>
     */
    @Comment("子路径[文件除开根路径的子路径][1:本地文件根路径=uploadDir,2=s3根路径=桶]")
    @Column(columnDefinition = "varchar(512)")
    private String subPath;

    /**
     * 完整的文件URL
     */
    @Comment("文件URL")
    @Column(columnDefinition = "varchar(1024) not null")
    private String url;

    /**
     * 文件URL后缀[去除了url中的域名或者ip,那个在一些场合可以由前端自定义]
     */
    @Comment("文件URL后缀[去除了url中的域名或者ip,那个在一些场合可以由前端自定义]")
    @Column(columnDefinition = "varchar(1024) not null")
    private String urlSuffix;


    /**
     * 存储的桶
     */
    @Comment("存储的桶")
    @Column(columnDefinition = "varchar(100)")
    private String bucket;

    /**
     * 存储的桶
     */
    @Comment("文件在桶中的路径[桶加子路径和本身]")
    @Column(columnDefinition = "varchar(100)")
    private String bucketPath;

    /**
     * 文件类型[contentType]
     */
    @Comment("文件类型[contentType]")
    @Column(columnDefinition = "varchar(128)")
    private String type;

    /**
     * 文件大小
     */
    @Comment("文件大小")
    @Column(columnDefinition = "varchar(128)")
    private String size;


    /**
     * 文件大小[GB,MB,KB]
     */
    @Comment("文件大小[GB,MB,KB]")
    @Column(columnDefinition = "varchar(128)")
    private String sizeUnit;


    /**
     *  FileStorage -> FileStorageVO
     */
    public FileIndexVO toFileStorageVO(){
        FileIndexVO vo = new FileIndexVO();
        vo.setId(getId());
        vo.setStorageId(storageId);
        vo.setStorage(storage);
        vo.setOriginalName(originalName);
        vo.setFreshName(freshName);
        vo.setPath(path);
        vo.setSubPath(subPath);
        vo.setUrl(url);
        vo.setBucket(bucket);
        vo.setBucketPath(bucketPath);
        vo.setUrlSuffix(urlSuffix);
        vo.setType(type);
        vo.setSize(size);
        vo.setSizeUnit(sizeUnit);
        if(null != getCreateTime()){
            vo.setCreateTime(getCreateTime().format(DateTimeFormatter.ofPattern(DEFAULT_FORMAT_DATETIME)));
        }else {
            vo.setCreateTime(DateTime.now().toString());
        }
        vo.setCreateUserName(getCreateUserName());
        return vo;
    }

    /**
     *  FileIndexMeta -> FileIndex
     */
    public FileIndex toFileIndex(){
        FileIndex fileIndex = new FileIndex();
        fileIndex.setId(getId());
        fileIndex.setStorageId(storageId);
        fileIndex.setStorage(storage);
        fileIndex.setOriginalName(originalName);
        fileIndex.setFreshName(freshName);
        fileIndex.setPath(path);
        fileIndex.setSubPath(subPath);
        fileIndex.setUrl(url);
        fileIndex.setBucket(bucket);
        fileIndex.setBucketPath(bucketPath);
        fileIndex.setUrlSuffix(urlSuffix);
        fileIndex.setType(type);
        if(size==null){
            fileIndex.setSize(0L);
        }else {
            fileIndex.setSize(Long.valueOf(size));
        }
        fileIndex.setSizeUnit(sizeUnit);
        return fileIndex;
    }


    /**
     *  FileIndex -> FileIndexMeta
     */
    public static FileIndexMeta toFileIndexMeta(FileIndex fileIndex){
        FileIndexMeta indexMeta = new FileIndexMeta();
        indexMeta.setStorageId(fileIndex.getStorageId());
        indexMeta.setStorage(fileIndex.getStorage());
        indexMeta.setOriginalName(fileIndex.getOriginalName());
        indexMeta.setFreshName(fileIndex.getFreshName());
        indexMeta.setPath(fileIndex.getPath());
        indexMeta.setSubPath(fileIndex.getSubPath());
        indexMeta.setUrl(fileIndex.getUrl());
        indexMeta.setBucket(fileIndex.getBucket());
        indexMeta.setBucketPath(fileIndex.getBucketPath());
        indexMeta.setUrlSuffix(fileIndex.getUrlSuffix());
        indexMeta.setType(fileIndex.getType());
        if(fileIndex.getSize()==null){
            indexMeta.setSize("0");
        }else {
            indexMeta.setSize(fileIndex.getSize()+"");
        }
        indexMeta.setSizeUnit(fileIndex.getSizeUnit());
        return indexMeta;
    }





}
